﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Famofo.Agbara.Core.FreeSwitch;
using System.Net.Sockets;
using System.Net;

namespace Famofo.Agbara.Sample.OutboundSample
{
    public class Sample1
    {
        public static void OutboundSample()
        {
            IPEndPoint address = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8084);
            OutboundServerTest server = new OutboundServerTest(address);
            server.serve_forever();
        }
    }
    public class OutboundTest : OutboundSocket
    {
        public OutboundTest(Socket sock, string filter = "ALL")
            : base(sock, filter)
        {
            base.OnCHANNEL_EXECUTE_COMPLETE += new EventHandlers(on_channel_execute_complete);
            base.OnCHANNEL_HANGUP_COMPLETE += new EventHandlers(on_channel_hangup_complete);
            base.OnCHANNEL_ANSWER += new EventHandlers(on_channel_answer);
        }

        private void on_channel_execute_complete(Event ev)
        {
            
            Console.WriteLine(string.Format("application done {0}", ev.get_header("Application")));

            if (ev.get_header("Application") == "play_and_get_digits")
            {
                
                Console.WriteLine("Added event to queue");
            }


        }
        private void on_channel_hangup_complete(Event evt)
        {
                Console.WriteLine("channel complete with {0}", evt.EventName);
        }
        private void on_channel_answer(Event ev)
        {
            Console.WriteLine("Call answered");
        }
        public override void run()
        {
            base.connect();
            foreach (string header in get_channel().get_headers().Keys.Cast<string>())
            {
                Console.WriteLine("{0} = {1}", header, get_channel().get_header(header));
            }
            //only catch events for this channel
            myevents();
            
            answer();
            Console.WriteLine("Wait answer");
            
            sleep("100");
            Console.WriteLine("Channel answered");
            // speak
            string soundfile = "C:/sounds/en/us/callie/conference/8000/conf-pin.wav";
            //playback(soundfile);
           // string text = "Please record your message for mr seun and seun famofo"; //"This is to inform you that you are now in agbara system.Please help us call spread the word around again";
            string bishoptext = "Welcome back mr Toba, your message continues now. To end the message, please press pounds key";
            string speak_args = string.Format("{0}|{1}|{2}", "flite", "slt", bishoptext);
            //string say_args = string.Format("{0} {1} {2} {3}", "en", "NUMBERS", "ITERATED", "123456789");
           // say(say_args);
            speak(speak_args);
            // play file
            //string beep = "tone_stream://%(300,200,700)";
            string play_str = "silence_stream://10";
            //string soundfile = "C:/sounds/en/us/callie/conference/8000/conf-pin.wav";
            //string invalidsound = "C:/sounds/en/us/callie/conference/8000/conf-bad-pin.wav";
            string bishop = "C:/sounds/bishop.wav";
            string filepath = "C:/sounds/test.wav";
             playback(play_str);
             playback(bishop,"#",get_channel_unique_id(),true,1);
             string bishoptext2 = "Thank you for tesing our service";
             string speak_args2 = string.Format("{0}|{1}|{2}", "flite", "slt", bishoptext2);
           // play_and_get_digits(2, 2, 3, 5000, "#", soundfile, invalidsound, "pagd_input", "0123456789", "", false);
            // record(filepath, "60", "500", "15", "123456789*#");
            Console.WriteLine("Done Playing music");
            //playback(filepath,"*");
            //finally hangup
            //hangup();

        }
    }

    public class OutboundServerTest : OutboundServer
    {
        public OutboundServerTest(IPEndPoint address)
            : base(address)
        {

            Console.WriteLine("Server Started ...");
        }
        public override void handle_request(Socket socket)
        {

            OutboundTest test = new OutboundTest(socket);
        }
    }
}
